package redis

import (
	"context"
	"fmt"
)

func AddCart(cart map[string]interface{}) error {
	key := fmt.Sprintf("cart_goods_id_%d", cart["goods_id"])
	return redisClient.HMSet(context.Background(), key, cart).Err()
}

func DeleteCart(goodsId int) bool {
	key := fmt.Sprintf("cart_goods_id_%d", goodsId)
	return redisClient.Del(context.Background(), key).Err() == nil
}

func UpdateCart(goodsId int) error {
	key := fmt.Sprintf("cart_goods_id_%d", goodsId)
	return redisClient.HSet(context.Background(), key).Err()
}

func ListCart(userId int, goodsId int) {
	key := fmt.Sprintf("cart:user_id_%d:goods_id_%d", userId, goodsId)
	redisClient.HMGet(context.Background(), key).Val()
}

func CheckedCart(goodsId int) (checked int, err error) {
	key := fmt.Sprintf("cart_goods_id_%d", goodsId)
	redisClient.HMSet(context.Background(), key, "checked", checked).Val()
	return
}

func TotalCart(goodsId int) (count, num int, err error) {
	key := fmt.Sprintf("cart_goods_id_%d", goodsId)
	keys := redisClient.HGetAll(context.Background(), key).Val()
	for i := 0; i < len(keys); i++ {
		count = +num
	}
	return
}
